package trabajoISW2.domain.implementations;

import java.util.Calendar;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import trabajoISW2.domain.contracts.IUser;
import trabajoISW2.domain.exceptions.WrongNameException;

@Entity
public class User implements IUser {

	@Id
	private String userId;
	private String name;
	private String surname;
	private String email;
	private Gender gender;
	private String password;
	
	@Temporal(TemporalType.DATE)
	private Calendar birthdate;
	
	@OneToMany (cascade = CascadeType.ALL)
	@JoinTable(name="User_Address",
			joinColumns= @JoinColumn(name="userId"),inverseJoinColumns=@JoinColumn(name="addressId"))
	private List<Address> addresses;
	
	@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
	private List<CreditCard> cards;
	
	public User(String userId,String name,String surname,String email,Gender gender,String password,Calendar birthdate){
		this.userId = userId;
		this.name = name;
		this.surname = surname;
		this.email = email;
		this.gender = gender;
		this.password = password;
		this.birthdate = birthdate;
	}
	
	public User(){
	}
	
	public User(String userId2, String name2, String surname2, String email2,
			Gender gender2, String password2, Calendar birthdate2,
			List<CreditCard> cards2) {
		this.userId = userId2;
		this.name = name2;
		this.surname = surname2;
		this.email = email2;
		this.gender = gender2;
		this.password = password2;
		this.birthdate = birthdate2;
	}


	@Override
	public String getUserId() {
		return userId;
	}

	@Override
	public void setUserId(String userId) {
		if(userId == null | userId == "")
			throw new WrongNameException();
		else
			this.userId = userId;
	}

	@Override
	public String getName() {
		return name;
	}

	@Override
	public void setName(String name) {
		if(name == null | name == "")
			throw new WrongNameException();
		else
			this.name = name;
	}

	@Override
	public String getSurname() {
		return surname;
	}

	@Override
	public void setSurname(String surname) {
		if(surname == null | surname == "")
			throw new WrongNameException();
		else
			this.surname = surname;
	}

	@Override
	public String getEmail() {
		return email;
	}

	@Override
	public void setEmail(String email) {
		if(email == null | email == "")
			throw new WrongNameException();
			else
		this.email = email;
	}

	@Override
	public Gender getGender() {
		return gender;
	}

	@Override
	public void setGender(Gender gender) {
		if(gender == null)
			throw new IllegalArgumentException();
			else
				this.gender = gender;
	}

	@Override
	public String getPassword() {
		return password;
	}

	@Override
	public void setPassword(String password) {
		if(password == null | password == "")
			throw new WrongNameException();
		else
			this.password = password;
	}

	@Override
	public Calendar getBirthdate() {
		return birthdate;
	}

	@Override
	public void setBirthdate(Calendar birthdate) {
		Calendar c = Calendar.getInstance();
		if(birthdate.after(c))
			throw new IllegalArgumentException();
		else
			this.birthdate = birthdate;
	}

	@Override
	public List<Address> getAddresses() {
		return addresses;
	}

	@Override
	public void setAddresses(List<Address> addresses) {
		if(addresses == null)
			throw new IllegalArgumentException();
		else
			this.addresses = addresses;
	}

	public void setCards(List<CreditCard> cards) {
		if(cards == null)
			throw new IllegalArgumentException();
		else
			this.cards = cards;
	}

	public List<CreditCard> getCards() {
		return cards;
	}
	
	@Override
	public void addCreditCard(CreditCard cc) {
		if(cc == null)
			throw new IllegalArgumentException();
		else
			cards.add(cc);
	}
	
	@Override
	public void addAddress(Address ad) {
		if(ad == null)
			throw new IllegalArgumentException();
		else
			addresses.add(ad);
	}
	
	@Override
	public void removeCreditCard(CreditCard cc) {
		if(cc == null)
			throw new IllegalArgumentException();
		else
			cards.remove(cc);
	}
	
	@Override
	public void removeAddress(Address ad) {
		if(ad == null)
			throw new IllegalArgumentException();
		else
			addresses.remove(ad);
	}
	public boolean equals (Object o){
		boolean b = false;
		User u = (User) o;
		if (getUserId().equals(u.getUserId()))
			b = true;
		return b;
	}

}
